package com.quad.innovators.salesease.controller;

import com.quad.innovators.salesease.annotation.DataPermission;
import com.quad.innovators.salesease.annotation.FunctionPermission;
import com.quad.innovators.salesease.annotation.Log;
import com.quad.innovators.salesease.annotation.RepeatSubmit;
import com.quad.innovators.salesease.common.result.PageResultBean;
import com.quad.innovators.salesease.common.result.Result;
import com.quad.innovators.salesease.constants.SystemConstants;
import com.quad.innovators.salesease.enums.LogModuleEnum;
import com.quad.innovators.salesease.model.common.Option;
import com.quad.innovators.salesease.model.form.SysRoleForm;
import com.quad.innovators.salesease.model.query.SysRolePageQuery;
import com.quad.innovators.salesease.model.vo.SysRolePageVO;
import com.quad.innovators.salesease.service.SysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "03.角色接口")
@RestController
@RequestMapping(SystemConstants.BACKEND_API +  "/roles")
@RequiredArgsConstructor
public class SysRoleController {

    private final SysRoleService sysRoleService;

    @Operation(summary = "角色分页列表")
    @GetMapping("/page")
    @Log( value = "角色分页列表",module = LogModuleEnum.ROLE)
    @DataPermission(permissionCode = "sys:role:data-permission")
    public Result<PageResultBean<SysRolePageVO>> getRolePage(SysRolePageQuery queryParams) {
        PageResultBean<SysRolePageVO> result = sysRoleService.getRolePage(queryParams);
        return Result.success(result);
    }

    @Operation(summary = "角色下拉列表")
    @GetMapping("/options")
    @DataPermission(permissionCode = "sys:role:data-permission")
    public Result<List<Option<String>>> listRoleOptions() {
        List<Option<String>> list = sysRoleService.listRoleOptions();
        return Result.success(list);
    }

    @Operation(summary = "新增角色")
    @PostMapping
    @FunctionPermission(permissionCode = "sys:role:add")
    @RepeatSubmit
    public Result<?> addRole(@Valid @RequestBody SysRoleForm roleForm) {
        boolean result = sysRoleService.saveRole(roleForm);
        return Result.judge(result);
    }

    @Operation(summary = "角色表单数据")
    @GetMapping("/{roleCode}/form")
    @Parameters({
            @Parameter(name = "roleCode", description = "角色ID")
    })
    @FunctionPermission(permissionCode = "sys:role:detail")
    @DataPermission(permissionCode = "sys:role:data-permission")
    public Result<SysRoleForm> getRoleForm(@PathVariable String roleCode) {
        SysRoleForm roleForm = sysRoleService.getRoleForm(roleCode);
        return Result.success(roleForm);
    }

    @Operation(summary = "修改角色")
    @PutMapping(value = "/{roleCode}")
    @FunctionPermission(permissionCode = "sys:role:edit")
    public Result<?> updateRole(@PathVariable("roleCode") String roleCode, @Valid @RequestBody SysRoleForm roleForm) {
        boolean result = sysRoleService.updateRole(roleCode, roleForm);
        return Result.judge(result);
    }

    @Operation(summary = "删除角色")
    @DeleteMapping("/{roleCodes}")
    @FunctionPermission(permissionCode = "sys:role:delete")
    @Parameters({
            @Parameter(name = "roleCodes", description = "删除角色，多个以英文逗号(,)拼接")
    })
    public Result<?> deleteRoles(@PathVariable String roleCodes) {
        boolean result = sysRoleService.deleteRoles(roleCodes);
        return Result.judge(result);
    }

    @Operation(summary = "修改角色状态")
    @PutMapping(value = "/{roleCodes}/status")
    @FunctionPermission(permissionCode = "sys:role:status")
    @Parameters({
            @Parameter(name = "roleCodes", description = "角色id", required = true),
            @Parameter(name = "status", description = "状态(1:启用;0:禁用)")
    })
    public Result<?> updateRoleStatus(@PathVariable String roleCodes,@RequestParam Integer status) {
        boolean result = sysRoleService.updateRoleStatus(roleCodes, status);
        return Result.judge(result);
    }

    @Operation(summary = "获取角色的菜单ID集合")
    @GetMapping("/{roleCodes}/menuIds")
    @Parameters({
            @Parameter(name = "roleCodes", description = "角色id", required = true)
    })
    @DataPermission(permissionCode = "sys:role:data-permission")
    public Result<List<String>> getRoleMenuIds(@PathVariable String roleCodes) {
        List<String> menuIds = sysRoleService.getRoleMenuIds(roleCodes);
        return Result.success(menuIds);
    }

    @Operation(summary = "分配菜单(包括按钮权限)给角色")
    @PutMapping("/{roleCodes}/menus")
    @Parameters({
            @Parameter(name = "roleCodes", description = "角色id", required = true),
            @Parameter(name = "menuCodes", description = "菜单id列表", required = true)
    })
    public Result<?> assignMenusToRole(@PathVariable String roleCodes, @RequestBody List<String> menuCodes) {
        boolean result = sysRoleService.assignMenusToRole(roleCodes, menuCodes);
        return Result.judge(result);
    }
}
